package timing.ukulele.form.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.form.persistent.EntityFieldConfigDraft;
import timing.ukulele.form.service.EntityFieldConfigDraftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/entityConfigFieldDraft")
@Api(tags = "实体字段配置草稿")
@Slf4j
public class EntityFieldConfigDraftController {

    final EntityFieldConfigDraftService service;

    public EntityFieldConfigDraftController(EntityFieldConfigDraftService service) {
        this.service = service;
    }

    /**
     * 新增修改草稿字段
     *
     * @param entityFieldConfigDraft
     * @return
     */
    @PostMapping("/save")
    public ResponseResult<EntityFieldConfigDraft> saveEntityFieldConfigDraft(
            @RequestBody EntityFieldConfigDraft entityFieldConfigDraft) {
        return service.saveData(entityFieldConfigDraft);
    }

    /**
     * 修改组的字段类型
     *
     * @param config 组对象，组名称与字段类型
     * @return
     */
    @PutMapping("/modify")
    public ResponseResult<EntityFieldConfigDraft> modify(
            @RequestBody EntityFieldConfigDraft config) {
        if (config == null || config.getEntityConfigDraftId() == null || StringUtils.isEmpty(config.getField()))
            return ResponseResult.error("参数不完整");
        if (!(config.getType() == 0 || config.getType() == 1 || config.getType() == 2)) {
            return ResponseResult.error("类型参数取值不规范");
        }
        return service.saveData(config);
    }

    @DeleteMapping("/{id}")
    public ResponseResult<String> delete(@PathVariable("id") Long id) {
        service.removeById(id);
        return ResponseResult.success();
    }


    /**
     * 查询当前草稿包含的所有字段
     *
     * @return
     */
    @GetMapping("/page")
    public ResponseResult<IPage<EntityFieldConfigDraft>> page(@ApiParam(value = "草稿id", example = "1") @RequestParam(value = "configDraftId") Long configDraftId,
                                                              @ApiParam(value = "描述", example = "djfodjf") @RequestParam(value = "description", required = false) String description,
                                                              @ApiParam(value = "字段", example = "dofajofd") @RequestParam(value = "field", required = false) String field,
                                                              @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
                                                              @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize) {
        return service.page(configDraftId, description, field, pageNum, pageSize);
    }

}
